#include "EmotionBar.h"
#include "Renderer.h"
#include <sstream>

using namespace irr; 
using namespace core;
using namespace gui; 
using namespace video; 
using namespace scene; 
using namespace std;

CEmotionBar::CEmotionBar()
{
	CRenderer*		pRender = CRenderer::Instance();
	ISceneNode*		pSceneNode;

	m_nSentences = 0;

	for(int i = 0; i < NUMOFBAR; i++)
	{
		stringstream	ss;

		pSceneNode = pRender->AddBox(vector3df(-168 - 6.5 * i, -50, 0), 
									 vector3df(3, 3, 2), SColor(255, 200, 0, 0));

		pSceneNode->setVisible(false);
		ss << "e_" << i;
		pSceneNode->setName(ss.str().c_str());

		m_aEmotions[0] = 0;
	}
}

void CEmotionBar::SetEmotions(std::vector<float> emotions)
{
	m_nSentences = emotions.size();

	for(int i = 0; i < m_nSentences; i++)
	{
		if(emotions[i] > 0)
			m_aEmotions[i] = 1;
		else if (emotions[i] == 0)
			m_aEmotions[i] = 0;
		else
			m_aEmotions[i] = -1;
	}
	Update();
}

void CEmotionBar::Update()
{
	CRenderer*		pRender		= CRenderer::Instance();
	ISceneManager*	pSceneMgr	= pRender->SceneManager();
	ISceneNode*		pSceneNode;

	for(int i = 0; i < NUMOFBAR; i++)
	{
		stringstream ss;
		SColor		 color;

		ss << "e_" << i;
		pSceneNode = pSceneMgr->getSceneNodeFromName(ss.str().c_str());

		if(i < m_nSentences)
		{
			switch(m_aEmotions[i])
			{
				case 1 : color = SColor(255, 255, 100, 100);
					break;

				case 0 : color = SColor(255, 150, 150, 150);
					break;

				case -1 : color = SColor(255, 100, 100, 255);
					break;
			}

			pSceneNode->setMaterialFlag(EMF_NORMALIZE_NORMALS, true);
			pSceneNode->setMaterialTexture(0, pRender->AddTextureFromSColor(color, "box", 
											 dimension2d<u32>(32, 32)));
			pSceneNode->setVisible(true);
		}
		else
		{
			pSceneNode->setVisible(false);
		}
	}
}

CEmotionBar::~CEmotionBar()
{
}


